import { loginWithMsg ,loginWithPass ,loginOut , verifyUserStatus} from "@/api/user"
import { debounce } from "../../utils/debounce"
import * as responseStatus from '@/utils/constant.js'


const saveState = () => {
  localStorage.setItem('userInfo', JSON.stringify(state));
}

const debounceFunc = debounce(saveState,1000)

const state = localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')) : {
  token: '',
  birthday: '',
  userId: '',
  userName: '',
  selfIntroduction: '',
  avatar: '',
  sex: '',
  registerEmail: '',
  registerPhoneNumber: '',
  id: 0,
}

const actions = {
  // remove token
  resetToken({ commit }) {

    return new Promise(resolve => {
      commit('SET_BIRTHDAY','')
      commit('SET_USERNAME','')
      commit('SET_USERID','')
      commit('SET_AVATAR','')
      commit('SET_SELFIntroduction','')
      commit('SET_TOKEN','')
      commit('SET_SEX','')
      commit('SET_REGISTER_EMAIL','')
      commit('SET_REGISTER_PHONE_NUMBER','');
      commit('SET_ID',0);
      resolve()
    })
  },

  accountLoginWithPass({commit},form){

    let {username,password}= form;

    return new Promise((resolve,reject) => {
      loginWithPass({username: username.trim(),password: password}).then(res => {

        if(res.code == responseStatus.SUCCESS){
          commit('SET_BIRTHDAY',res.data.birthday)
          commit('SET_USERNAME',res.data.userName)
          commit('SET_USERID',res.data.userId)
          commit('SET_AVATAR',res.data.avatar)
          commit('SET_SELFIntroduction',res.data.selfIntroduction)
          commit('SET_TOKEN',res.data.token)
          commit('SET_SEX',res.data.sex)
          commit('SET_REGISTER_EMAIL',res.data.registerEmail)
          commit('SET_REGISTER_PHONE_NUMBER',res.data.registerPhoneNumber);
          commit('SET_ID',res.data.id);
          resolve();
        }
      })
      .catch(err => reject(err));
    })
  },
  accountLoginWithMsg({commit},form){

    let {username,code}= form;

    return new Promise((resolve,reject) => {
      loginWithMsg({username: username.trim(), code: code}).then(res => {

        if(res.code == responseStatus.SUCCESS){
          commit('SET_BIRTHDAY',res.data.birthday)
          commit('SET_USERNAME',res.data.userName)
          commit('SET_USERID',res.data.userId)
          commit('SET_AVATAR',res.data.avatar)
          commit('SET_SELFIntroduction',res.data.selfIntroduction)
          commit('SET_TOKEN',res.data.token)
          commit('SET_SEX',res.data.sex)
          commit('SET_REGISTER_EMAIL',res.data.registerEmail)
          commit('SET_REGISTER_PHONE_NUMBER',res.data.registerPhoneNumber);
          commit('SET_ID',res.data.id);
          resolve();
        }
      })
      .catch(err => reject(err));
    })
  },
  loginOut({commit}){
    return new Promise((resolve, reject) =>{
      loginOut(state.token).then(res => {
        commit('SET_BIRTHDAY','')
        commit('SET_USERNAME','')
        commit('SET_USERID','')
        commit('SET_AVATAR','')
        commit('SET_SELFIntroduction','')
        commit('SET_TOKEN','')
        commit('SET_SEX','')
        commit('SET_REGISTER_EMAIL','')
        commit('SET_REGISTER_PHONE_NUMBER','');
        commit('SET_ID',null);
        resolve();
      }).catch(err => {reject(err);})
    })
  },

  verifyUser({commit}){
    return new Promise((resolve, reject) =>{
      verifyUserStatus(state.token).then(res => {
        resolve(res);
      })
      .catch(err => reject(err))
    })
  }
}

const getters = {
  token: state => state.token,
  birthday: state => state.birthday,
  userId: state => state.userId,
  userName: state => state.userName,
  selfIntroduction: state => state.selfIntroduction,
  avatar: state => state.avatar,
  sex: state => state.sex,
  registerEmail: state => state.registerEmail,
  registerPhoneNumber: state => state.registerPhoneNumber,
  id: state => state.id,
}

const mutations = {
  SET_BIRTHDAY(state,birthday){
    state.birthday = birthday;
    debounceFunc.apply(null,[]);
  },
  SET_USERNAME(state,userName){
    state.userName = userName;
    debounceFunc.apply(null,[]);
  },
  SET_USERID(state,userId){
    state.userId = userId;
    debounceFunc.apply(null,[]);
  },
  SET_AVATAR(state,avatar){
    state.avatar = avatar;
    debounceFunc.apply(null,[]);
  },
  SET_SELFIntroduction(state,selfIntroduction){
    state.selfIntroduction = selfIntroduction;
    debounceFunc.apply(null,[]);
  },
  SET_TOKEN(state,token){
    state.token = token;
    debounceFunc.apply(null,[]);
  },
  SET_SEX(state,sex){
    state.sex = sex;
    debounceFunc.apply(null,[]);
  },
  SET_REGISTER_EMAIL(state,registerEmail){
    state.registerEmail = registerEmail;
    debounceFunc.apply(null,[]);
  },
  SET_REGISTER_PHONE_NUMBER(state,registerPhoneNumber){
    state.registerPhoneNumber = registerPhoneNumber;
    debounceFunc.apply(null,[]);
  },
  SET_ID(state,id){
    state.id = id;
    debounceFunc.apply(null,[]);
  },
}
export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
